package se.jocke.nb.proxy.ui.node;

import java.util.HashMap;
import java.util.Map;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.lookup.Lookups;
import se.jocke.nb.proxy.monitor.model.HttpMessage;

/**
 *
 * @author jocke
 */
public class HttpMessageNode extends AbstractNode {

    private final HttpMessage httpMessage;

    private static final Map<String, String> CONTENT_TYPE_TO_ICON_MAP = new HashMap<String, String>();

    public static final String ICON_PATH = "se/jocke/nb/proxy/ui/node/icon/";

    static {
        CONTENT_TYPE_TO_ICON_MAP.put("application/atom+xml", "xmlObject.gif");
        CONTENT_TYPE_TO_ICON_MAP.put("application/x-www-form-urlencoded", "htmlEdit.gif");
        CONTENT_TYPE_TO_ICON_MAP.put("application/json", "javascript.png");
        CONTENT_TYPE_TO_ICON_MAP.put("application/octet-stream", "txtObject.gif");
        CONTENT_TYPE_TO_ICON_MAP.put("application/svg+xml", "xmlObject.gif");
        CONTENT_TYPE_TO_ICON_MAP.put("text/html", "htmlEdit.gif");
        CONTENT_TYPE_TO_ICON_MAP.put("application/xhtml+xml", "htmlEdit.gif");
        CONTENT_TYPE_TO_ICON_MAP.put("application/xml", "xmlObject.gif");
        CONTENT_TYPE_TO_ICON_MAP.put("text/plain", "txtObject.gif");
    }

    public HttpMessageNode(HttpMessage httpMessage) {
        super(Children.LEAF, Lookups.singleton(httpMessage));
        super.setShortDescription(httpMessage.toString());
        this.httpMessage = httpMessage;
        final String contentType = httpMessage.getResponseHeaders().get("Content-Type");
        if (contentType != null) {
            if (contentType.startsWith("img") || contentType.startsWith("image")) {
                setIconBaseWithExtension(ICON_PATH + "imageObject.gif");
            } else if (CONTENT_TYPE_TO_ICON_MAP.containsKey(contentType)) {
                setIconBaseWithExtension(ICON_PATH + CONTENT_TYPE_TO_ICON_MAP.get(contentType));
            }
        }
    }

    public HttpMessage getHttpMessage() {
        return httpMessage;
    }

    @Override
    public String getHtmlDisplayName() {
        return "<font color='" + getColor() + "'>" + httpMessage.getMethod() + "->" + httpMessage.getPath() + "</font>";
    }

    public String getHtmlDisplayStatus() {
        return "<font color='" + getStatusColor() + "'>" + getStatusCode() + " " + getStatusMessage() + "</font>";
    }

    private String getColor() {

        if (httpMessage.getStatusCode() >= 400) {
            return "#FC0000";
        } else {
            return "#000000";
        }
    }

    private String getStatusColor() {
        if (httpMessage.getStatusCode() >= 400) {
            return "#FC0000";
        } else {
            return "#C6C7C3";
        }
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 53 * hash + (this.httpMessage != null ? this.httpMessage.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final HttpMessageNode other = (HttpMessageNode) obj;
        return this.httpMessage == other.httpMessage || (this.httpMessage != null && this.httpMessage.equals(other.httpMessage));
    }

    @Override
    public String toString() {
        return "HttpMessageNode{" + "httpMessage=" + httpMessage + '}';
    }

    private String getStatusCode() {
        return httpMessage.getStatusCode() == 0 ? "" : Integer.toString(httpMessage.getStatusCode());
    }

    private String getStatusMessage() {
        return httpMessage.getReasonPhrase() == null ? "" : httpMessage.getReasonPhrase();
    }

}
